// commpents/select/select.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    selectList: Array,
    multiple: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    value: [],
    valueLable: '',
    hidden: true
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    open(){
      this.setData({hidden: false})
    },
    close(){
      this.setData({hidden: true})
    },
    selectTap(e){
      if(this.data.multiple){
        let valueId = this.data.value.map(i=>{return i.id})
        let oldValue = this.data.value
        if(valueId.indexOf(e.target.dataset.id) == -1){
          oldValue.push({id: e.target.dataset.id, name: e.target.dataset.name})
        }else {
          oldValue.splice(valueId.indexOf(e.target.dataset.id), 1)
        }
        let valueName = oldValue.map(i=>{return i.name}).join()
        this.setData({
          value: oldValue,
          valueLable: valueName
        })
      }else{
        let oldValue = []
        oldValue.push({id: e.target.dataset.id, name: e.target.dataset.name})
        this.setData({
          value: oldValue,
          valueLable: oldValue[0].name
        })
      }
      this.triggerEvent('getSelectList', this.data.value)
    }
  }
})